package com.vision.sales.salesman.cl.ui;

import static com.vision.sales.cl.SalesServices.salesmanSvc;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.form.validator.MaxLengthValidator;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.ui.UI;
import com.vision.sales.cl.event.SalesmanCreatedEvent;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cm.db.data.Salesman;
import com.vision.sales.cm.db.data.Salesman.FieldSpecs;

/**
 * 
 * @author Mark
 *
 */
public class CreateSalesmanWindow implements IsWidget, Editor<Salesman> {
	
	interface Binder extends UiBinder<Widget, CreateSalesmanWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField TextField code;
	@UiField TextField name;
	@UiField TextField phone;
	@UiField TextArea address;
	@UiField TextArea comment;
	
	// Ignored by the form driver since these are not fields in the Salesman
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;
	
	@UiField Window window;
	
	interface SalesmanDriver extends SimpleBeanEditorDriver<Salesman, CreateSalesmanWindow> { }
	
	private SalesmanDriver driver = GWT.create(SalesmanDriver.class); // Used to bind the grid to the form editor
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(SalesResources.INSTANCE.salesmen());
		
		driver.initialize(this);
		driver.edit(new Salesman());
		
		code.addValidator(new MaxLengthValidator(FieldSpecs.CODE_MAX));
		name.addValidator(new MaxLengthValidator(FieldSpecs.NAME_MAX));
		address.addValidator(new MaxLengthValidator(FieldSpecs.ADDRESS_MAX));
		phone.addValidator(new MaxLengthValidator(FieldSpecs.PHONE_MAX));
		comment.addValidator(new MaxLengthValidator(FieldSpecs.COMMENT_MAX));
		
		return window;
	}
	
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		Salesman salesman = driver.flush();
		if (driver.hasErrors())
			return;
		
		UI.mask(window, "Creating salesman...");
		salesmanSvc().create(salesman, new Callback<Salesman>(window) {
			@Override
			protected void handleSuccess(Salesman result) {
				window.hide(saveBtn);
				
				BaseModule.fireEvent(new SalesmanCreatedEvent(result));
			}
		});
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		window.hide(cancelBtn);
	}

}
